package com.tro.web.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.socket.TextMessage;

import com.tro.common.util.upload.UploadUtil;
import com.tro.mail.service.MailScoketHandler;
import com.tro.web.bean.IndexMenuBean;
import com.tro.web.bean.StartMenuBean;
import com.tro.web.dao.entity.Permission;
import com.tro.web.dao.entity.PermissionExample;
import com.tro.web.dao.mapper.PermissionMapper;

@Controller
@RequestMapping("/index")
public class IndexController {
	@RequestMapping("/index")
	public String toIndex() {
		return "/hello";
	}
	@Autowired
	MailScoketHandler handler;
	@RequestMapping("/test")
	public String toTest(){
		try {
			handler.sendMessageToUser("admin", new TextMessage("nihao"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping("/shortcut")
	public String toShortcut() {
		return "/shortcut";
	}
	
	@RequestMapping("/mail")
	public String mail() {
		return "/mail/writMail";
	}

	@RequestMapping("app")
	@ResponseBody
	public List<IndexMenuBean> getDesktop(HttpServletRequest request) {
		List<IndexMenuBean> result = new ArrayList<>();
		IndexMenuBean menu = new IndexMenuBean();
		for (int i = 0; i < 5; i++) {
			menu = new IndexMenuBean();
			menu.setId(i);
			menu.setHref("http://www.baidu.com");
			menu.setIcon(request.getContextPath() + "/static/index/images/computer.png");
			menu.setText("我的电脑--" + i);
			result.add(menu);
		}

		return result;
	}
	@Autowired
	PermissionMapper permission;
	
	@RequestMapping("starMenu")
	@ResponseBody
	public List<StartMenuBean> getStarMenu(HttpServletRequest request) {
		List<StartMenuBean> result = new ArrayList<>();
		
		PermissionExample search=new PermissionExample();
		search.createCriteria().andPidEqualTo(new BigDecimal("0")).andTypeEqualTo("F");
		List<Permission> menus=permission.selectByExample(search);
		StartMenuBean start=new StartMenuBean();
		StartMenuBean children=new StartMenuBean();
		List<StartMenuBean> childrens=new ArrayList<>();
		for(Permission p:menus){
			start=new StartMenuBean();
			childrens=new ArrayList<>();
			start.setId(p.getId());
			start.setHref("");
			start.setIconCls(p.getIcon());
			start.setText(p.getName());
			search=new PermissionExample();
			search.createCriteria().andPidEqualTo(new BigDecimal(p.getId()+"")).andTypeEqualTo("F");
			List<Permission> childs=permission.selectByExample(search);
			for(Permission c:childs){
				children=new StartMenuBean();
				children.setId(c.getId());
				children.setHref(c.getUrl());
				children.setIconCls(c.getIcon());
				children.setText(c.getName());
				childrens.add(children);
			}
			start.setChildren(childrens);
			result.add(start);
		}
		return result;
	}

	@RequestMapping("updateDeskTop")
	public String upDeskTop( HttpServletRequest request,Model model) {
		String saveUrl = request.getContextPath() + "/attached/";
		model.addAttribute("backgroundUrl",saveUrl+"wallpaper.jpg");
		model.addAttribute("message","" );
		return "/desktop";
	}

	@RequestMapping("uploadImg")
	public String imgUpload(HttpServletRequest request, Model model,String backgroundUrl) {

		List<MultipartFile> fileList = new ArrayList<>();
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			fileList = multipartRequest.getFiles("imgFile");
		} catch (Exception e) {
			e.printStackTrace();
		}
		UploadUtil upload = new UploadUtil();
		String saveUrl = request.getContextPath() + "/attached/";
		Map<String, Object> result = upload.imageUpload(saveUrl, "image", fileList);
		if((int)result.get("error")==0){
			model.addAttribute("backgroundUrl",result.get("url") );
			model.addAttribute("message","" );
		}else{
			model.addAttribute("backgroundUrl",backgroundUrl);
			model.addAttribute("message",result.get("message") );
		} 
		return "/desktop";
	}
	
	public String permissionList(){
		return "";
	}
}
